import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/data/entities/home/analysis/get_company_device_list_entity.dart';
import 'package:safetymonitor/generated/assets.dart';

import 'off_controller.dart';

///离线设备
class OffPage extends StatelessWidget {
  OffPage({super.key});

  final controller = Get.put(OffController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: AppColors.bg_color_grey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<GetCompanyDeviceListEntity>(
                        enableLoad: false,
                        key: controller.globalKey,
                        dataLoad: controller.getCompanyDeviceListEntity,
                        itemBuilder: (item, index) {
                          return _itemWidget(item);
                        },
                      ))
                ],
              ),
            ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
            borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              Assets.svgSearch,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller:
                    TextEditingController(text: controller.searchX.value),
                    onChanged: (value) {
                      controller.searchX.value = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(
                        color: const Color(0xff222222),
                        fontWeight: FontWeight.w400,
                        fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border:
                      const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入设备名称',
                      hintStyle: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                          color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  /// 标题
  Widget _title(String title, String? value) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            color: const Color(0xffA5A6B5),
            fontSize: 13.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
        if (value != null)
          Expanded(
              child: Text(
                value,
                style: TextStyle(
                  color: const Color(0xff495061),
                  fontSize: 13.sp,
                  fontWeight: FontWeight.w400,
                ),
              )),
      ],
    );
  }

  Widget _itemWidget(GetCompanyDeviceListEntity item) {
    return CustomTitleChartPage(
        title: item.deviceName ,
        child: Column(
          children: [
            Row(
              children: [
                Expanded(child: _title('系统名称:', item.systemName)),
                Expanded(child: _title('监测类型:', item.monitorName)),
              ],
            ),
          ],
        ));
  }
}
